import { fetchFishingSpotList } from "../../api/fishing-spots";
import { delay } from "../../utils/index";

Page({
  /**
   * 页面的初始数据
   */
  data: {
    empty: false,
  },

  async onShow() {
    const app = getApp();
    console.log("====guide page load=======");
    await app.globalData.readyPromise;
    console.log("guide page load, globalData:", app.globalData);
    const { userInfo, spotId } = app.globalData;
    if (!userInfo?.phone) {
      wx.navigateTo({
        url: "/pages/login/login",
      });
      return;
    }
    if (!spotId) {
      spotId = await app.initSpotData();
    }
    if (spotId) {
      console.log("重定向到个人中心");
      wx.switchTab({
        url: "/pages/usercenter/index",
      });
      return;
    }
    this.setData({
      empty: true,
    });
  },

  // 加载钓场列表
  async loadFishingSpotList() {
    console.log("==== loadFishingSpotList=======");

    wx.showLoading({
      mask: true, // 可选，添加透明蒙层防止触摸穿透
    });
    await delay(300); // 模拟加载延迟
    let list = [];
    try {
      wx.hideLoading();
      list = await fetchFishingSpotList();
    } catch (error) {
      wx.hideLoading();
      console.error("加载钓场列表失败:", error);
    }
    if (list.length > 0) {
      console.log("重定向到个人中心");
      wx.switchTab({
        url: "/pages/usercenter/index",
      });
      return;
    }
    this.setData({
      empty: list.length === 0,
    });
  },

  onAddFishingSpot() {
    wx.navigateTo({
      url: "/pages/fishingspot/setting/setting",
    });
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
